﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using IGolovkine.ShoutCastRipper.Service.Contracts;
using System.Timers;
using System.Windows;
using System.Windows.Threading;

namespace IGolovkine.ShoutCastRipper.UI.AdminPanel
{
    [ServiceBehavior(IncludeExceptionDetailInFaults=true)]
    public class AdminPanelWindowController : AdminServiceCallBackContract
    {
        private readonly AdminPanelModel _model;
        private readonly IAdminServiceContract _adminService;
        private readonly Timer _timer;
        private readonly Dispatcher _dispatcher;

        public AdminPanelWindowController(Dispatcher dispatcher)
        {
            _model = new AdminPanelModel();
            _dispatcher = dispatcher;
            try
            {
                DuplexChannelFactory<IAdminServiceContract> factory = new DuplexChannelFactory<IAdminServiceContract>(new InstanceContext(this), new NetTcpBinding());
                _adminService = factory.CreateChannel(new EndpointAddress("net.tcp://barton:1235/StationRipperAdminService"));
                _timer = new Timer(1000);
                _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
                _timer.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                _dispatcher.Invoke(new Action(this.Refresh));
            }
            catch
            {
            }
        }

        public AdminPanelModel Model
        {
            get { return _model; }
        }

        public void Refresh()
        {
            try
            {
                _model.UpdateStationRipperStatusItems(_adminService.GetStatuses());
                _model.UpdateStationRipperEventItems(_adminService.GetEvents(DateTime.Now.AddHours(-1)));

            }
            catch
            {
                _model.UpdateStationRipperStatusItems(new StationRipperStatusDataContract[0]);
                _model.UpdateStationRipperEventItems(new StationRipperEventDataContract[0]);
            }
        }

        public void SkipCurrentTitleClick(string name)
        {
            _adminService.SkipCurrentTitle(name);
        }

        public void StopAfterCurrentTitleClick(string name)
        {
            _adminService.StopAfterCurrentTitle(name);
        }

        public void Stop(string name)
        {
            _adminService.Stop(name);
        }

        public void Start(string name)
        {
            _adminService.Start(name);
        }

        #region AdminServiceCallBackContract Members

        public void OnStationRipperStatusChanged(string name)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
